#!/usr/bin/env bash

source /usr/share/kwort/common/default

function install_ndis() {
	if [ -z "${1}" ]; then
		ALERT_MESSAGE="<vbox><pixmap><input file>${ERROR_IMG48}</input></pixmap><text use-markup=\"true\"><label>\"<b>You must select a driver</b>\"</label></text><button ok><input file>${OK_IMG}</input></button></vbox>"
		export ALERT_MESSAGE
		gtkdialog --program=ALERT_MESSAGE >/dev/null
		return 1
	fi
	if [[ ${1} != *.[iI][nN][fF] ]]; then
		ALERT_MESSAGE="<vbox><pixmap><input file>${ERROR_IMG48}</input></pixmap><text use-markup=\"true\"><label>\"<b>Windows driver must end in .inf</b>\"</label></text><button ok><input file>${OK_IMG}</input></button></vbox>"
		export ALERT_MESSAGE
		gtkdialog --program=ALERT_MESSAGE >/dev/null
		return 1
	fi
	ndiswrapper -i "${1}" >/dev/null 2>&1
	ret=$?
	if [ ${ret} -eq 25 ]; then
		ALERT_MESSAGE="<vbox><pixmap><input file>${ERROR_IMG48}</input></pixmap><text use-markup=\"true\"><label>\"<b>Driver is already installed</b>\"</label></text><button ok><input file>${OK_IMG}</input></button></vbox>"
		export ALERT_MESSAGE
		gtkdialog --program=ALERT_MESSAGE >/dev/null
		return 0
	elif [ ${ret} -ne 0 ]; then
		ALERT_MESSAGE="<vbox><pixmap><input file>${ERROR_IMG48}</input></pixmap><text use-markup=\"true\"><label>\"<b>An unknown error ocurred while installing the driver</b>\"</label></text><button ok><input file>${OK_IMG}</input></button></vbox>"
		export ALERT_MESSAGE
		gtkdialog --program=ALERT_MESSAGE >/dev/null
		return 0
	fi
	return 0
}

export MAIN_DIALOG="
<window title=\"Kwort Network Manager\" icon-name=\"${KNM_IMG}\" window_position=\"1\" resizable=\"false\">
	<vbox>
		<frame>
			<hbox>
				<pixmap>
					<input file>${BIG_WIRELESS_CARD}</input>
				</pixmap>
				<vbox>
					<text wrap=\"true\">
						<label>If you wireless device is not supported by Linux,</label>
					</text>
					<text wrap=\"true\">
						<label>Kwort will use NDISwrapper to allow the proper</label>
					</text>
					<text wrap=\"true\">
						<label>Windows driver to work under Linux.</label>
					</text>
					<text wrap=\"true\">
						<label>To make your wireless card work in Kwort, install</label>
					</text>
					<text wrap=\"true\">
						<label>your windows .inf driver here. When you have</label>
					</text>
					<text wrap=\"true\">
						<label>have finished, click on \"Go Back\"</label>
					</text>
				</vbox>
			</hbox>
		</frame>
		<frame Choose your windows driver>
			<hbox>
				<entry accept=\"savefilename\">
					<label>Select a inf Filename</label>
					<variable>FILE_SAVEFILENAME</variable>
				</entry>
				<button>
					<input file>${FILE_OPEN_IMG}</input>
					<action type=\"fileselect\">FILE_SAVEFILENAME</action>
				</button>
			</hbox>
		</frame>
		<hbox>
			<button>
				<label>Install and leave</label>
				<input file>${EXIT_IMG}</input>
			</button>
			<button>
				<label>Install and follow</label>
				<input file>${REDO_IMG}</input>
			</button>
			<button>
				<label>Go Back</label>
				<input file>${BACK_IMG}</input>
			</button>
		</hbox>
	</vbox>
</window>"

FORWARD=no
while [ "${FORWARD}" == "no" ]; do
	eval $(gtkdialog --program=MAIN_DIALOG)
	if [ "${EXIT}" == "Go Back" -o "${EXIT}" == "abort" ]; then
		exit
	fi
	install_ndis "${FILE_SAVEFILENAME}"
	if [ $? -eq 0 -a "${EXIT}" == "Install and leave" ]; then
		exit
	fi
done

